package com.example.carproviderbackstage.service.impl.dl.admin;

import com.alibaba.dubbo.config.annotation.Service;
import com.example.carinterface.service.dl.admin.BannerService;
import com.example.carinterface.utils.ResultJson;
import com.example.carproviderbackstage.mapper.dl.admin.BannerMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class BannerServiceImpl implements BannerService {

    @Autowired
    private BannerMapper bannerMapper;
    @Autowired
    private ResultJson<List<Map<String, Object>>> resultJson;
    @Autowired
    private ResultJson<String> stringResultJson;

    @Override
    public ResultJson<List<Map<String, Object>>> findAll() {


        List<Map<String, Object>> all = bannerMapper.findAll();
        ResultJson<List<Map<String, Object>>> resultJson = new ResultJson<>();
        resultJson.setSuccess("查询成功", all);


        return resultJson;
    }

    @Override
    public ResultJson<String> delById(Integer id) {
        ResultJson<String> resultJson = new ResultJson<>();


        if (bannerMapper.delById(id) == 1) {
            resultJson.setSuccess("删除成功");
        } else {
            resultJson.setSuccess("删除失败");
        }

        return resultJson;
    }

    @Override
    public ResultJson<Map<String, Object>> findById(Integer id) {
        ResultJson<Map<String, Object>> resultJson = new ResultJson<>();

        Map<String, Object> byId = bannerMapper.findById(id);
        resultJson.setSuccess("查询成功", byId);
        return resultJson;
    }

    @Override
    public ResultJson<String> updateBanner(Map<String, Object> map) {
        int i = bannerMapper.updateBanner(map);
        if (i == 1) {
            stringResultJson.setSuccess("修改成功");
        } else {
            stringResultJson.setSuccess("修改失败");
        }

        return stringResultJson;
    }

    @Override
    public ResultJson<Map<String, String>> addBanner(Map<String, Object> map) {
        ResultJson<Map<String, String>> resultJson = new ResultJson<>();
        Integer maxSort = bannerMapper.findMaxSort();
        if (maxSort==null){
            maxSort=0;
        }
        map.put("sort", maxSort + 1);

        int i = bannerMapper.addBanner(map);
        if (i == 1) {
            resultJson.setSuccess("添加成功");
        } else {
            resultJson.setSuccess("删除失败");
        }
        return resultJson;
    }

    @Override
    public ResultJson<String> moveBanner(List<Map<String, Object>> list) {
        ResultJson<String> resultJson = new ResultJson<>();

        if (list == null || list.size() != 2) {
            resultJson.setSuccess("移动失败");
            return resultJson;
        }
        int i = bannerMapper.moveBanner(list);
        if (i == 2) {
            resultJson.setSuccess("移动成功");
        } else {
            resultJson.setSuccess("移动失败");
        }

        return resultJson;
    }

    @Override
    public ResultJson<String> moveBanner(Map<String, Object> list) {
        String to = list.get("to").toString();
        Map<String, Object> t=null;
        //向下
        if (to.equals("1")) {
           t= bannerMapper.findGtBanner(list);
            //向上
        } else if (to.equals("-1")){
          t = bannerMapper.findLtBanner(list);
        }

        if (t==null){
            stringResultJson.setSuccess("交换失败");
        }else {
            //可以交换
            Object sort = list.get("sort");

            Object sort1 = t.get("sort");

            list.put("sort",sort1);
            t.put("sort",sort);

            List<Map<String, Object>> list1 = new ArrayList<>();
            list1.add(list);
            list1.add(t);
            int i = bannerMapper.moveBanner(list1);
            if (i==2){
                stringResultJson.setSuccess("交换成功");
            }
        }

        return stringResultJson;
    }
}
